package com.zycfc.zsf.boot.redis.util;

import com.zycfc.zsf.boot.redis.constant.*;
import com.zycfc.zsf.boot.redis.autoconfigure.*;
import org.apache.commons.codec.binary.*;
import com.zycfc.zsf.boot.util.time.*;
import java.util.*;
import org.apache.commons.lang3.*;
import java.io.*;
import org.slf4j.*;

public class RedisLocalFileUtil
{
    private static final Logger log;
    private static final String SPLIT_REGEX = "\\|";
    
    public static void writeToFile() {
        File redisFile = null;
        FileWriter writer = null;
        try {
            final File fileDir = new File(RedisConstant.REDIS_FILE_DIR);
            redisFile = new File(RedisConstant.REDIS_FILE_PATH);
            if (!fileDir.exists()) {
                synchronized (RedisOperaterAutoConfigure.class) {
                    if (!fileDir.exists()) {
                        fileDir.mkdirs();
                        redisFile.createNewFile();
                    }
                }
            }
            if (!redisFile.exists()) {
                synchronized (RedisOperaterAutoConfigure.class) {
                    if (!redisFile.exists()) {
                        redisFile.createNewFile();
                    }
                }
            }
        }
        catch (Throwable e) {
            RedisLocalFileUtil.log.error("\u521b\u5efaredis\u672c\u5730\u6587\u4ef6Exception:", e);
        }
        String key = null;
        try {
            if (redisFile != null) {
                writer = new FileWriter(redisFile, true);
                final OutputStream out = new FileOutputStream(redisFile);
                for (final Map.Entry<String, byte[]> entry : RedisConstant.redisDataMap.entrySet()) {
                    key = entry.getKey();
                    final byte[] valueBytes = entry.getValue();
                    final String value = (valueBytes != null) ? Base64.encodeBase64String(valueBytes) : "";
                    writer.write(key + "|" + value + "|" + DateUtils.getCurDateTimeNoSep() + "\r\n");
                }
                writer.write("######");
            }
        }
        catch (Throwable e2) {
            RedisLocalFileUtil.log.error("redis\u5185\u5bb9\u5199\u5165\u672c\u5730\u6587\u4ef6\u5f02\u5e38\uff0ckey:{},Exception:", (Object)key, (Object)e2);
            if (writer != null) {
                try {
                    writer.close();
                }
                catch (Throwable e2) {
                    RedisLocalFileUtil.log.error("FileWriter close Exception:", e2);
                }
            }
        }
        finally {
            if (writer != null) {
                try {
                    writer.close();
                }
                catch (Throwable e3) {
                    RedisLocalFileUtil.log.error("FileWriter close Exception:", e3);
                }
            }
        }
    }
    
    public static byte[] getFromFile(final String key) {
        BufferedReader reader = null;
        byte[] result = null;
        final long beginTime = System.currentTimeMillis();
        int i = 1;
        try {
            final File file = new File(RedisConstant.REDIS_FILE_PATH);
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while (true) {
                while (i <= 500000) {
                    ++i;
                    final String lineText = reader.readLine();
                    if (StringUtils.isEmpty((CharSequence)lineText)) {
                        continue;
                    }
                    if ("######".equals(lineText)) {
                        RedisLocalFileUtil.log.info("\u8bfb\u53d6\u672c\u5730redis\u6587\u4ef6\u5230\u4e86\u672b\u5c3e:{}", (Object)lineText);
                    }
                    else {
                        final String[] arr = lineText.split("\\|");
                        if (!key.equals(arr[0])) {
                            continue;
                        }
                        result = (byte[])(com.zycfc.zsf.boot.util.text.StringUtils.isNotEmpty(arr[1]) ? Base64.decodeBase64(arr[1]) : null);
                    }
                    final long endTime = System.currentTimeMillis();
                    final long dur = endTime - beginTime;
                    RedisLocalFileUtil.log.info("\u83b7\u53d6key{}\u5185\u5bb9\u8bfb\u53d6\u884c\u6570{}\u8017\u65f6{}", new Object[] { key, i, dur });
                    return result;
                }
                continue;
            }
        }
        catch (Throwable e) {
            final long end = System.currentTimeMillis();
            final long dur = end - beginTime;
            RedisLocalFileUtil.log.error("\u83b7\u53d6key{}\u5185\u5bb9\u8bfb\u53d6\u884c\u6570{}\u8017\u65f6{},Exception:", new Object[] { key, i, dur, e });
            if (reader != null) {
                try {
                    reader.close();
                }
                catch (Throwable e) {
                    RedisLocalFileUtil.log.error("reader close Exception:", e);
                }
            }
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                }
                catch (Throwable e2) {
                    RedisLocalFileUtil.log.error("reader close Exception:", e2);
                }
            }
        }
        return null;
    }
    
    public static byte[] getObjectBytes(final Object obj) {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream objOut = null;
        byte[] objectBytes = null;
        try {
            objOut = new ObjectOutputStream(out);
            objOut.writeObject(obj);
            objectBytes = out.toByteArray();
        }
        catch (IOException e) {
            RedisLocalFileUtil.log.error("obj\u8f6c\u5b57\u8282\u6d41\u53d1\u751fException:", (Throwable)e);
            if (objOut != null) {
                try {
                    objOut.close();
                }
                catch (IOException e) {
                    RedisLocalFileUtil.log.error("\u5173\u95edObjectOutputStream\u53d1\u751fException:", (Throwable)e);
                }
            }
        }
        finally {
            if (objOut != null) {
                try {
                    objOut.close();
                }
                catch (IOException e2) {
                    RedisLocalFileUtil.log.error("\u5173\u95edObjectOutputStream\u53d1\u751fException:", (Throwable)e2);
                }
            }
        }
        return objectBytes;
    }
    
    public static Object getObject(final byte[] objectBytes) {
        final ByteArrayInputStream in = new ByteArrayInputStream(objectBytes);
        ObjectInputStream objIn = null;
        Object obj = null;
        try {
            objIn = new ObjectInputStream(in);
            obj = objIn.readObject();
        }
        catch (IOException ex) {}
        catch (ClassNotFoundException e) {
            RedisLocalFileUtil.log.error("\u4ece\u5b57\u8282\u6d41\u8bfb\u53d6object\u53d1\u751fException:", (Throwable)e);
            if (objIn != null) {
                try {
                    objIn.close();
                }
                catch (IOException e2) {
                    RedisLocalFileUtil.log.error("\u5173\u95edObjectInputStream\u53d1\u751fException:", (Throwable)e2);
                }
            }
        }
        finally {
            if (objIn != null) {
                try {
                    objIn.close();
                }
                catch (IOException e3) {
                    RedisLocalFileUtil.log.error("\u5173\u95edObjectInputStream\u53d1\u751fException:", (Throwable)e3);
                }
            }
        }
        return obj;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)RedisLocalFileUtil.class);
    }
}
